<?php

/**
 * this class provides functions for working with jquery easyui datagrids
 * @author		ungerboeck simon <su@militaerversand.at>
 * @link        http://www.jeasyui.com/documentation/index.php
 */
class DataGrid {
    private $id = null;
    private $title = null;
    private $height = null;
    private $width = null;
    private $fit = null;
    private $border = null;
    private $singleSelect = null;
    private $headerColumns = array();
    private $dataRows = array();
    private $rows = null;
    private $fitColumns = null;
    private $rowClickFunc = null;
    private $rowDblClickFunc = null;
    private $rowSortFunc = null;
    private $onClickCellFunc = null;
    private $rowStyleFunc = null;
    private $checkOnSelect = null;


    /************************************************************************
     ************************** PUBLIC FUNCTIONS ****************************
     ************************************************************************/

    /**
     * sets the id for this datagrid
     * @param         string         $id
     */
    public function __construct($id = null) {
        $this->id = $id;
        $this->fit = true;
        $this->border = false;
        $this->singleSelect = true;
        $this->fitColumns = false;
        $this->onCheckSelect = true;
    }


    /**
     * adds one header column to datagrid
     * @param         mixed         $headerColumn
     */
    public function addHeaderColumn(DataColumn $headerColumn) {
        if ($headerColumn != null)
            array_push($this->headerColumns, $headerColumn);
    }


    /**
     * adds a data row to current grid
     * @param         mixed         $dataRow
     */
    public function addDataRow(DataRow $dataRow) {
        if ($dataRow != null)
            array_push($this->dataRows, $dataRow);
    }


    /**
     * creates the grid
     * @return         string        $output
     */
    public function create() {
        $output = '<table class="easyui-datagrid" ';
        $output .= (isset($this->id) ? 'id="' . $this->id . '" ' : '');
        $output .= (isset($this->title) ? 'title="' . $this->title . '" ' : '');
        $output .= $this->getStyleOptions();
        $output .= $this->getDataOptions();
        $output .= '>';
        $output .= $this->createHeaderColumns();
        $output .= $this->createRows();
        $output .= '</table>';
        return $output;
    }
    
    
    /**
     * converts the obj to a string output
     * @return         string
     */
    public function __toString() {
        return $this->create();
    }
    
    
    /**
     * enables multiple selection
     */
    public function enableMultipleSelect() {
    	$this->singleSelect = false;
    }
    
    
    public function disableCheckOnSelect(){
    	$this->checkOnSelect = false;
    }
    


    /************************************************************************
     **************************** GETTER / SETTER ***************************
     ************************************************************************/

    public function setId($id) {
        $this->id = $id;
    }


    public function setTitle($title) {
        $this->title = $title;
    }


    public function setHeight($height) {
        if (is_numeric($height))
            $this->height = $height;
        else
            throw new Exception('your given height value is not numeric!');
    }


    public function setWidth($width) {
        if (is_numeric($width))
            $this->width = $width;
        else
            throw new Exception('your given width value is not numeric!');
    }


    public function setSizes($width, $height) {
        if (is_numeric($width) and is_numeric($height)) {
            $this->height = $height;
            $this->width = $width;
        } else
            throw new Exception('one or more of your given height and width values are not numeric!');
    }


    public function setFit($fit) {
        $this->fit = $fit;
    }


    public function setBorder($border) {
        $this->border = $border;
    }

    public function setFitColumns($fitColumns) {
        $this->fitColumns = $fitColumns;
    }
    
    
    public function setRowClickFunction($function){
        $this->rowClickFunc = $function;
    }
    
    
    public function setRowDblClickFunction($function){
        $this->rowDblClickFunc = $function;
    }
    
    
    public function setRowSortFunc($function){
    	$this->rowSortFunc = $function;
    }
    
    
    public function setOnClickCellFunc($function){
    	$this->onClickCellFunc = $function;
    }
    
    public function setRowStyleFunc($function){
    	$this->rowStyleFunc = $function;
    }


    public function getId() {
        return $this->id;
    }


    public function getTitle() {
        return $this->title;
    }


    public function getHeight() {
        return $this->height;
    }


    public function getWidth() {
        return $this->width;
    }


    public function getFit() {
        return $this->fit;
    }


    public function getBorder() {
        return $this->border;
    }


    public function isSingleSelect() {
        return $this->singleSelect();
    }


    public function isFitColumns() {
        return $this->fitColumns;
    }
    
    
    public function getRowClickFunction(){
        return $this->rowClickFunc;
    }
    
    
    public function getRowDblClickFunction(){
        return $this->rowDblClickFunc;
    }
    
    
    public function getRowSortFunc(){
    	return $this->rowSortFunc;
    }


    /************************************************************************
     ************************** PRIVATE FUNCTIONS ***************************
     ************************************************************************/

    /**
     * returns the output for data options
     * @return         string        $output
     */
    private function getDataOptions() {
        $output = 'data-options="striped: true, idField:\'id\', ';
        $output .= (isset($this->fit) ? 'fit:' . (($this->fit) ? 'true,' : 'false,') : '');
        $output .= (isset($this->border) ? 'border:' . (($this->border) ? 'true,' : 'false,') : '');
        $output .= (isset($this->singleSelect) ? 'singleSelect:' . (($this->singleSelect) ? 'true,' : 'false,') : '');
        $output .= (isset($this->fitColumns) ? 'fitColumns:' . (($this->fitColumns) ? 'true,' : 'false,') : '');
        $output .= (isset($this->rowClickFunc) ? 'onClickRow:' . $this->rowClickFunc . ',' : '');
        $output .= (isset($this->rowSortFunc) ? 'onSortColumn:' . $this->rowSortFunc . ',' : '');
        $output .= (isset($this->onClickCellFunc) ? 'onClickCell:' . $this->onClickCellFunc . ',' : '');
        $output .= (isset($this->rowDblClickFunc) ? 'onDblClickRow:' . $this->rowDblClickFunc . ',' : '');
        $output .= (isset($this->rowStyleFunc) ? 'rowStyler:' . $this->rowStyleFunc . ',' : '');
        $output .= (isset($this->checkOnSelect) ? 'checkOnSelect:' . (($this->checkOnSelect) ? 'true,' : 'false,') : '');
        $output .= '" ';
        return $output;
    }


    /**
     * returns the style option for this datagrid
     * @return         string        $output
     */
    private function getStyleOptions() {
        $output = '';
        $output .= ((isset($this->width) or isset($this->height)) ? 'style="' . (isset($this->width) ? 'width:' . $this->width . 'px; ' : '') . (isset($this->height) ? 'height:' . $this->height . 'px' : '') . '" ' : '');
        $output .= ' ';
        return $output;
    }


    /**
     * creates all header columns of current datagrid
     * @return         string        $output
     */
    private function createHeaderColumns() {
        $output = '<thead>';
        $output .= '<tr>';
        foreach ($this->headerColumns as $column)
            $output .= $column->create();
        $output .= '</tr>';
        $output .= '</thead>';
        return $output;
    }


    /**
     * creates all datarows
     * @return         string        $output
     */
    private function createRows() {
        $output = '<tbody>';
        foreach ($this->dataRows as $dataRow) {
            $output .= '<tr>';
            foreach ($dataRow->getCells() as $dataCell)
                $output .= $dataCell->create();
            $output .= '</tr>';
        }
        $output .= '</tbody>';
        return $output;
    }
}